package com.hopu.blb_erp.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hopu.blb_erp.entity.LaserSecurity;
import com.hopu.blb_erp.entity.Product;
import com.hopu.blb_erp.entity.User;
import com.hopu.blb_erp.service.IProductService;
import com.hopu.blb_erp.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;
import com.hopu.blb_erp.entity.Product;
import com.hopu.blb_erp.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.List;

@Api
/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author A-Zhang
 * @since 2021-11-24
 */
@RestController
public class ProductController {
    @Autowired
    private IProductService productService;



    @GetMapping("/laserSecurity")
    public ResponseEntity<LaserSecurity> findLaser(String code){
        LaserSecurity laserSecurity = productService.findLaserSecurity(code);

        return ResponseEntity.ok(laserSecurity);
    }

    @GetMapping("/companyProducts")
    public ResponseEntity<List<Product>> findProductByCompany(Integer id){
        List<Product> list = productService.list(new QueryWrapper<Product>().eq("company_id",id));

        return ResponseEntity.ok(list);
    }
    @GetMapping("/products")
    public ResponseEntity<List<Product>> findAll(){
        List<Product> list = productService.list();
        return ResponseEntity.ok(list);
    }

    @ApiOperation("模糊查询和分页")
    @GetMapping("/product/like")
    public ResponseEntity<Page<Product>> findLikeProduct(Integer current, Integer pageSize, Long companyId, String pEnterprise ){
        if ("".equals(pEnterprise) || "null".equals(pEnterprise)){
            pEnterprise = null;
        }
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(companyId != null,"company_id",companyId)
                .like(StringUtils.isNotBlank(pEnterprise),"p_enterprise",pEnterprise);
        Page<Product> productPage = new Page<>(current,pageSize);
        Page<Product> page = productService.page(productPage,queryWrapper);
        return  ResponseEntity.ok(page);
    }

    @ApiOperation("按照id查询商品")
    @GetMapping("/product/{pNumber}")
    public ResponseEntity<Product> findProductByPNumber(@PathVariable("pNumber")String pNumber){
        Product product = productService.getOne(new QueryWrapper<Product>().eq("p_number", pNumber));
        return ResponseEntity.ok(product);
    }
}
